package com.dalin.platform.autocode.action;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.dalin.framework.action.StrutsTwoAction;
import com.dalin.platform.autocode.service.AutocodeService;
import com.dalin.platform.autocode.vo.EditMenuTreeVo;
/*****************************************************************************
 * @content: 类信息                                                   *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-06-18                                                     *
 * @modDate: 2010-06-18                                                     *
 * @version: 1.00                                                           *
 ****************************************************************************/
public class MenuAddModDelAction extends StrutsTwoAction{
	private AutocodeService autocodeService; // 
	private String strTree;
	private Long[] delIdArray;
	
	public String execute() throws Exception {
        super.printInfo("", "===== this action begin");
        try{
        	JSONArray ja = JSONArray.fromObject(strTree);
    		List<EditMenuTreeVo> treeList = getListFromJSONArray(ja);
    		autocodeService.addUpDelMenus(treeList, delIdArray);
    		setResContent(true,"保存成功");
        } catch (Exception e) {
        	setResContent(e);
        }
        super.printInfo("", "===== this action end");
        return null;
    }
	
	private List<EditMenuTreeVo> getListFromJSONArray(JSONArray ja) {
		List<EditMenuTreeVo> rtnList = new ArrayList<EditMenuTreeVo>();
		JSONObject jo = null;
		EditMenuTreeVo treeVo = null;
		for (int i = 0; i < ja.size(); i++) {
			treeVo = new EditMenuTreeVo();
			jo = ja.getJSONObject(i);
			treeVo.setId(jo.getString("id")); // 结点编码
			treeVo.setMenuValue(Integer.parseInt(jo.getString("menuValue")));
			treeVo.setText(jo.getString("text")); // 结点名称
			treeVo.setHref(jo.getString("href")); // 链接
			treeVo.setParentId(jo.getString("parentId")); // 父结点ID
			treeVo.setRemark(jo.getString("remark")); // 备注
			rtnList.add(treeVo);
		}
		return rtnList;
	}



	public AutocodeService getAutocodeService() {
		return autocodeService;
	}

	public void setAutocodeService(AutocodeService autocodeService) {
		this.autocodeService = autocodeService;
	}

	public String getStrTree() {
		return strTree;
	}

	public void setStrTree(String strTree) {
		this.strTree = strTree;
	}

	public Long[] getDelIdArray() {
		return delIdArray;
	}

	public void setDelIdArray(Long[] delIdArray) {
		this.delIdArray = delIdArray;
	}
}
